// VC1EncASF.h : Declaration of the CVC1EncASF

#pragma once
#include "resource.h"       // main symbols
#include <assert.h>
#include <string.h>

#include "VC1EncoderBackend_i.h"
#include "ASFWriter.h"
#include "AVIReader.h"
#include "logger.h"
#include "vc1prosdk.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif


// Supported frame rates.
const double rgdFramerate[] = {23.976, 24.00, 25.0, 29.97, 50.0, 59.94}; 
const double rgdFRNumerator[] = {24000, 24, 25, 30000, 50, 60000};  
const double rgdFRDenominator[] = {1001, 1, 1, 1001, 1, 1001};

// CVC1EncASF

class ATL_NO_VTABLE CVC1EncASF :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CVC1EncASF, &CLSID_VC1EncASF>,
	public IDispatchImpl<IVC1EncASF, &IID_IVC1EncASF, &LIBID_VC1EncoderBackendLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
	    CVC1EncASF(); 
DECLARE_REGISTRY_RESOURCEID(IDR_VC1ENCASF)


BEGIN_COM_MAP(CVC1EncASF)
	COM_INTERFACE_ENTRY(IVC1EncASF)
	COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()



	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

public:

	STDMETHOD(Init)(CONFIG_PROPERTIES2 cfProperties, FILTERS2 filters, ME_PROPERTIES2 meProperties, QUANT_PROPERTIES2 qtProperties, BOOL bLookAhead, INT32 iVideoType, BSTR bsInputAVSFile, BSTR bsProfileFileName, BSTR bsOutputFileName);
	STDMETHOD(Encode)(INT32 functionPointer);
	STDMETHOD(Destroy)(void);

private:

	DWORD Analyze(INT32 functionPointer);

	BOOL get_userConfigProps();
    BOOL get_userAdvancedProps();
    DWORD get_advancedProps();
    DWORD set_advancedProps();
    DWORD ShowStatistics();
	CONFIG_PROPERTIES CopyToConfigProperties(CONFIG_PROPERTIES2 inputProperties);
	FILTERS CopyToFilters(FILTERS2 inputFilters);
	ME_PROPERTIES CopyToMEProperties(ME_PROPERTIES2 inputProperties);
	QUANT_PROPERTIES CopyToQuantProperties(QUANT_PROPERTIES2 inputProperties);

    HRESULT InitFileWriter();
    HRESULT InitFileReader();
    void DestroyFileWriter();
    void DestroyFileReader();

    HRESULT WriteAudioVideo(BYTE *pVC1Data, DWORD dwVC1DataSize, INT64 tTimeStampVideo, FrameType ft);
    BOOL GetFrameRateND(double dFrameRate, double *pdFRNumerator, double *pdFRDenominator);
    DWORD FlushVideo();
    HRESULT FlushAudio();

    ////// Member variables //////
	INT32 m_StatusInt;

    // VC-1 Encoder settings.
    COLOR_FORMAT      m_ColorFormat;
    CONFIG_PROPERTIES m_ConfigProps;
    FILTERS           m_Filters;
    ME_PROPERTIES     m_MEProps;
    QUANT_PROPERTIES  m_QuantProps;
    DWORD   m_dwBframeDeltaQP;
    BOOL    m_bClosedEntryPt;
    DWORD   m_dwDisplayHeight;
    DWORD   m_dwDisplayWidth;
    DWORD   m_dwEncodeHeight;
    DWORD   m_dwEncodeWidth;
    BOOL    m_bLBPresent;
    BOOL    m_bLookahead;
    BOOL    m_bAdaptiveGOP;
    DWORD   m_dwKeyPop;
    DWORD   m_dwNumThreads;
    DWORD   m_dwAffinityMask;
    DWORD   m_dwPixHeight;
    DWORD   m_dwPixWidth;
    DWORD   m_dwPixIndex;
    DWORD   m_dwVideoType;
    BOOL    m_bTFF; // top field first flag
    BOOL    m_bRFF; // repeat first field flag
 
    // Class variables.
    CVC1Encoder *m_pEncoder;
    CAVIFileReader  *m_pFileReader;
    CASFFileWriter  *m_pFileWriter;

    // File paths for media.
    WCHAR   m_wszInputFilePath[MAX_PATH];
    WCHAR   m_wszOutputFilePath[MAX_PATH];

    // File path of the .prx file used for generating the audio profile for compression. 
    // See IWMProfileManager::LoadProfileByData for details about using a profile file.
    WCHAR m_wszProfileFilePath [MAX_PATH];

    // Other member variables
    INT64   m_qwOutputSampleTime;
    FrameType m_FrameType;
    BOOL    m_bColorFormat; // TRUE when user sets COLOR_FORMAT value(s). 
    BOOL    m_bPARI; // TRUE when user sets PixelAspectRatioIndex.
    double  m_dFRNumerator;
    double  m_dFRDenominator;
    wchar_t **m_ppArgv; // Pointer to command line string pointer.
    int     m_iArgc;  // Args count.
    DWORD   m_dwOutputBufferSize; // Size of the VC-1 buffer.
    DWORD   m_dwVC1DataSize; // Size of the data in the buffer.
    BYTE    *m_pVC1Data;  // VC-1 data buffer pointer.
    BITMAPINFOHEADER *m_pBIH;
    WAVEFORMATEX *m_pWFX;

	HANDLE  m_hOutput;
};

OBJECT_ENTRY_AUTO(__uuidof(VC1EncASF), CVC1EncASF)
